import Vue from 'vue'
import Router from 'vue-router'
import {
  Message
} from 'element-ui'
import store from '../vuex/store'
import Login from '@/components/Login'
import Signup from '@/components/Signup'
import Home from '@/components/Home'
import dataTest from '@/components/dataTest/index'
import table from '@/components/dataTest/table'
import stuPic from '@/components/stuPic/index'
import stuDet from '@/components/stuPic/detail'
Vue.use(Router)
let router = new Router({
  routes: [{
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/signup',
      name: 'Signup',
      component: Signup
    },
    {
      path: '/',
      name: 'Login',
      component: Login,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/home/dataTest',
      meta: {
        requireAuth: true
      },
      children: [{
          path: '/home/dataTest',
          name: 'dataTest',
          component: dataTest,
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/table',
          name: 'table',
          component: table,
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/stuPic',
          name: 'stuPic',
          component: stuPic,
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/stuDet',
          name: 'stuDet',
          component: stuDet,
          meta: {
            requireAuth: true
          }
        }
      ]
    },
  ]
})
// router.beforeEach((to, from, next) => {
//   if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
//     if (store.state.user.name) { // 通过vuex state获取当前的token是否存在
//       next();
//     } else if (window.localStorage.getItem('current_user')) {
//       console.log(window.localStorage.getItem('current_user'))
//       store.commit('setUser', window.localStorage.getItem('current_user'))
//       next();
//     } else {
//       Message.warning("请先登录")
//       next({
//         path: '/login',
//         query: {
//           redirect: to.fullPath
//         } // 将跳转的路由path作为参数，登录成功后跳转到该路由
//       })
//     }
//   } else {
//     next();
//   }
// })
export default router
